package com.otomotion.research.ants.databank;

import java.util.Collection;
import java.util.HashMap;
import java.util.Vector;

import com.otomotion.research.ants.entities.Ant;
import com.otomotion.research.ants.information.Coordinate;
import com.otomotion.research.ants.util.IDFactory;
import com.otomotion.research.ants.util.LOGGER;

public class AntRepository 
{
    HashMap<Integer, Ant> byID;
    HashMap<String, Ant> byCoordinate;

    AntRepository() {
        LOGGER.logln("Creating AntRepository"+" ... ");
        byID = new HashMap<Integer, Ant>();
        byCoordinate = new HashMap<String, Ant>();
        LOGGER.log("DONE");
    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.AntRepositoryI#hasAnt(com.otomotion.research.ants.information.Coordinate)
     */
    public boolean hasAnt(Coordinate c)
    {
        return byCoordinate.get(c.toString())!=null;

    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.AntRepositoryI#getLocationOfAnt(com.otomotion.research.ants.entities.Ant)
     */
    public Coordinate getLocationOfAnt(Ant ant)
    {
        return ant.getLocation();

    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.AntRepositoryI#getIdleAnts()
     */
    public Collection<Ant> getIdleAnts()
    {
        return byID.values();
    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.AntRepositoryI#addAnt(com.otomotion.research.ants.information.Coordinate)
     */
    public boolean addAnt(Coordinate coordinate)
    {

        if (!this.hasAnt(coordinate))
        {
            Ant a = new Ant();
            a.setId(IDFactory.getID());
            byID.put(new Integer(a.getId()), a);
            this.byCoordinate.put(coordinate.toString(),a);
            a.setLocation(coordinate);
            return true;
        }
        return false;

    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.AntRepositoryI#moveAnt(com.otomotion.research.ants.entities.Ant, com.otomotion.research.ants.information.Coordinate)
     */
    public void moveAnt(Ant ant, Coordinate coordinate)
    {
        this.byCoordinate.put(ant.getLocation().toString(), null);
        ant.setLocation(coordinate);
        this.byCoordinate.put(coordinate.toString(),ant);

    }

	public Ant getAntById(int id) {
		return this.byID.get(new Integer(id));
	}
}
